package com.sdygmedia.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sdygmedia.domain.Article;
import com.sdygmedia.service.ArticleService;
import com.sdygmedia.service.CategoryChildService;
import com.sdygmedia.service.CategoryService;

@Controller
public class ArticleAction extends ActionSupport {

	private Article article;
	@Resource
	private ArticleService articleService;
	@Resource
	private CategoryService categoryService;
	@Resource
	private CategoryChildService categoryChildService;

	public String addUI() throws Exception {
		List categoryList = categoryService.findAll();
		List categoryChildList = categoryChildService.findAll();
		
		ActionContext.getContext().put("categoryList", categoryList);
		ActionContext.getContext().put("categoryChildList", categoryChildList);
		
		return "addUI";
	}

	public String add() throws Exception {
		articleService.save(article);
		
		return "add";
	}

	public String delete() throws Exception {
		articleService.delete(article.getId());
		
		return "toList";
	}

	public String updateUI() throws Exception {
		Article articleUp = articleService.getById(article.getId());
		List categoryList = categoryService.findAll();
		List categoryChildList = categoryChildService.findAll();
		
		ActionContext.getContext().put("categoryList", categoryList);
		ActionContext.getContext().put("categoryChildList", categoryChildList);
		ActionContext.getContext().put("articleUp", articleUp);
		
		return "updateUI";
	}

	public String update() throws Exception {
		articleService.update(article);
		
		return "update";
	}

	public String list() throws Exception {
		List<Article> articleList = articleService.findAll();
		ActionContext.getContext().put("articleList", articleList);
		
		return "list";
	}

	public String show() throws Exception {
		return "show";
	}

	// -------------------------------
	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public ArticleService getArticleService() {
		return articleService;
	}

	public void setArticleService(ArticleService articleService) {
		this.articleService = articleService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public CategoryChildService getCategoryChildService() {
		return categoryChildService;
	}

	public void setCategoryChildService(CategoryChildService categoryChildService) {
		this.categoryChildService = categoryChildService;
	}
}
